package com.gxa.modules.core;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.modules.dto.OrderFileVo;
import com.gxa.modules.entity.CrmOrder;
import com.gxa.modules.mapper.OrderMapper;
import com.gxa.modules.service.OrderFileService;
import com.gxa.modules.service.OrderService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Component
public class OrderFileExcelExport extends AbstractExcelExport<OrderFileVo>{
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderFileService orderFileService;

    /**
     * 小数据量数据导出excel
     */
    public void exportWithSmallData(HttpServletResponse response, String fileName, OrderFileVo orderFileVo){
        List<List<String>> head = new ArrayList<List<String>>();
        head.add(Collections.singletonList("编号"));
        head.add(Collections.singletonList("订单编号"));
        head.add(Collections.singletonList("客户名称"));
        head.add(Collections.singletonList("联系人"));
        head.add(Collections.singletonList("下单日期"));
        head.add(Collections.singletonList("交单日期"));
        head.add(Collections.singletonList("订单总金额"));
        head.add(Collections.singletonList("已收款"));
        head.add(Collections.singletonList("订单状态"));
        head.add(Collections.singletonList("发货状态"));
        head.add(Collections.singletonList("收货人"));
        head.add(Collections.singletonList("收货人电话"));
        head.add(Collections.singletonList("收货人地址"));
        head.add(Collections.singletonList("备注"));
        head.add(Collections.singletonList("业务员"));
        head.add(Collections.singletonList("录入时间"));


//查出总数据
        List<OrderFileVo> list= orderMapper.showAll(orderFileVo);

        this.exportSmallData(response,fileName,head,list);

    }
    //计算导出数据的总数
    @Override
    protected Integer dataTotalCount() {
        return Integer.valueOf(String.valueOf(orderMapper.selectCount(null)));
    }
    //每一个sheet存放的数据总数
    @Override
    protected Integer eachSheetTotalCount() {
        return 500000;
    }
    //每次写入sheet的总数
    @Override
    protected Integer eachTimesWriteSheetTotalCount() {
        return 5000;
    }
//分页批量导出
    @Override
    protected void buildDataList(List<List<String>> resultList, Map<String, Object> queryCondition, Integer pageNo, Integer pageSize) {
//分批导出（分页）
        System.out.println("分批导出（分页）分批导出（分页）分批导出（分页）分批导出（分页）分批导出（分页）");
        System.out.println(pageNo+"_"+pageSize);
        List<OrderFileVo> entityDoList = orderService.listPage(queryCondition,pageNo, pageSize);
        if(CollUtil.isNotEmpty(entityDoList)){
            entityDoList.forEach(item -> {
                resultList.add(Arrays.asList(String.valueOf(item.getOrderId()),String.valueOf(item.getOrderNo()),String.valueOf(item.getCustomerName()),
                        item.getEmployeeName(),String.valueOf(item.getTotalmony()),String.valueOf(item.getReceived()),String.valueOf(item.getGoodsName()),
                        String.valueOf(item.getOrderDate()),String.valueOf(item.getOrderRemarks()),
                        String.valueOf(item.getOrderStatus()),String.valueOf(item.getPresentationDate()),
                        String.valueOf(item.getReceiverName()),String.valueOf(item.getReceiverAdress()),
                        String.valueOf(item.getReceiverTelephone()),String.valueOf(item.getShipmentStatus()),String.valueOf(item.getEntryTime())
                ));

            });
}
    }
}
